﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using System.Windows.Forms;

namespace TemplateAddIn
{
    public partial class TemplateAddInUI : OfficeRibbon
    {
        public TemplateAddInUI()
        {
            InitializeComponent();
        }

        private void TemplateAddInUI_Load(object sender, RibbonUIEventArgs e)
        {
            Globals.ThisAddIn.VariableAdded += new VariableEvent(ThisAddIn_VariableAdded);
            Globals.ThisAddIn.VariableRemoved += new VariableEvent(ThisAddIn_VariableRemoved);
            Globals.ThisAddIn.VariableModified += new VariableEvent(ThisAddIn_VariableModified);
            Globals.ThisAddIn.VariableFieldSelected += new VariableEvent(ThisAddIn_VariableFieldSelected);
        }

        #region Variable Creation Events
        private void btnCreate_Click(object sender, RibbonControlEventArgs e)
        {
            new VariableCreationUI(Globals.ThisAddIn.Application.Selection.Range.Text).ShowDialog();
        }

        private void btnDelete_Click(object sender, RibbonControlEventArgs e)
        {
            if (ddlVariables.Items.Count > 0)
            {
                DialogResult dr = MessageBox.Show("Also delete the contents in the field?", "Confirmation", System.Windows.Forms.MessageBoxButtons.YesNoCancel);

                if (dr != DialogResult.Cancel)
                {
                    Globals.ThisAddIn.RemoveVariable(Globals.ThisAddIn.VarList[ddlVariables.SelectedItem.Label], dr == DialogResult.Yes ? true : false);
                }
            }
        }
        #endregion

        #region AddIn Events
        void ThisAddIn_VariableAdded(Variable sender)
        {
            RibbonDropDownItem rddItem = new RibbonDropDownItem();
            rddItem.Label = sender.Name;
            ddlVariables.Items.Add(rddItem);

            if (ddlVariables.Items.Count == 1)
            {
                ddlVariables.SelectedItemIndex = 0;

                if (sender.VariableType == Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlText)
                {
                    ttbVariableValue.Enabled = true;
                    ttbVariableValue.Text = sender.Content.ToString();
                }
                else
                {
                    ttbVariableValue.Enabled = false;
                    ttbVariableValue.Text = "";
                }
            }
        }

        void ThisAddIn_VariableRemoved(Variable sender)
        {            
                RibbonDropDownItem rddiRemove = null;

                foreach (RibbonDropDownItem rddItem in ddlVariables.Items)
                {
                    if (rddItem.Label == sender.Name)
                    {
                        rddiRemove = rddItem;
                    }
                }

                ddlVariables.Items.Remove(rddiRemove);
                if (ddlVariables.Items.Count == 0 || sender.VariableType != Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlText)            
                    ttbVariableValue.Text = "";
                else
                {
                    ddlVariables.SelectedItemIndex = 0;
                    if (Globals.ThisAddIn.VarList[ddlVariables.SelectedItem.Label].VariableType == Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlText)
                    {
                        ttbVariableValue.Enabled = true;
                        ttbVariableValue.Text = Globals.ThisAddIn.VarList[ddlVariables.SelectedItem.Label].Content.ToString();
                    }
                    else
                    {
                        ttbVariableValue.Enabled = false;
                        ttbVariableValue.Text = "";
                    }
                }
        }

        void ThisAddIn_VariableFieldSelected(Variable sender)
        {
            if (sender.VariableType == Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlText)
            {
                foreach (RibbonDropDownItem rddItem in ddlVariables.Items)
                {
                    if (rddItem.Label == sender.Name)
                    {
                        ddlVariables.SelectedItem = rddItem;

                        if (sender.VariableType == Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlText)
                        {
                            ttbVariableValue.Enabled = true;
                            ttbVariableValue.Text = sender.Content.ToString();
                        }
                        else
                        {
                            ttbVariableValue.Enabled = false;
                            ttbVariableValue.Text = "";
                        }
                    }
                }
            }
        }

        void ThisAddIn_VariableModified(Variable sender)
        {
            if (sender.VariableType == Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlText)
            {
                if (ddlVariables.SelectedItem.Label == sender.Name)
                {
                    if (sender.VariableType == Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlText)
                    {
                        ttbVariableValue.Enabled = true;
                        ttbVariableValue.Text = sender.Content.ToString();
                    }
                }
            }
        }
        #endregion

        #region Variable Edition Events
        private void ddlVariables_SelectionChanged(object sender, RibbonControlEventArgs e)
        {
            if (Globals.ThisAddIn.VarList[ddlVariables.SelectedItem.Label].VariableType == Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlText)
            {
                ttbVariableValue.Enabled = true;
                ttbVariableValue.Text = Globals.ThisAddIn.VarList[ddlVariables.SelectedItem.Label].Content.ToString();
            }
            else
            {
                ttbVariableValue.Enabled = false;
                ttbVariableValue.Text = "";
            }
        }

        private void ttbVariableValue_TextChanged(object sender, RibbonControlEventArgs e)
        {
            Globals.ThisAddIn.VarList[ddlVariables.SelectedItem.Label].SetContent(ttbVariableValue.Text);
        }

        private void btnAddVariableField_Click(object sender, RibbonControlEventArgs e)
        {
            if (ddlVariables.Items.Count > 0)
                Globals.ThisAddIn.InsertVariableControl(Globals.ThisAddIn.VarList[ddlVariables.SelectedItem.Label]);
        }

        private void btnRemoveField_Click(object sender, RibbonControlEventArgs e)
        {
            if (Globals.ThisAddIn.Application.Selection.Range.ContentControls.Count > 0)
            {
                DialogResult dr = MessageBox.Show("Also delete the contents in the field?", "Confirmation", System.Windows.Forms.MessageBoxButtons.YesNoCancel);

                if (dr != DialogResult.Cancel)
                    Globals.ThisAddIn.RemoveVariableControl(dr == DialogResult.Yes ? true : false);
            }
            else
            {
                MessageBox.Show("There are no variable fields in the selection!", "Warning!");
            }
        }

        private void btnVariableFill_Click(object sender, RibbonControlEventArgs e)
        {
            (new VariableFillUI(Globals.ThisAddIn.VarList)).ShowDialog();

        }
        #endregion
    }
}

